package com.shop2cn.iapi.agentproducts.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.GetProductStatusCountBean;
import com.shop2cn.iapi.agentproducts.parameter.ModifyAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.GetProductStatusCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.utils.AgentUsers;

/**
 * 获取各商品状态数量 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetProductStatusCount {
	private static GetProductStatusCountBean getproductstatuscountBean;
	private static GetProductStatusCountCall getproductstatuscountCall;
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取各商品状态数量");
	}

	@Before
	public void caseUp() {
		getproductstatuscountBean = new GetProductStatusCountBean();
		getproductstatuscountCall = new GetProductStatusCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductstatuscountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductStatusCount_001() {
		Logger.start(true, "获取商品状态");
		try {
			getproductstatuscountBean.setUserId(rootUserId);
			getproductstatuscountCall.setData(getproductstatuscountBean);
			getproductstatuscountCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductStatusCount_002() {
		Logger.start(true, "增加一个售磬商品，获取商品状态");
		try {
			getproductstatuscountBean.setUserId(rootUserId);
			getproductstatuscountCall.setData(getproductstatuscountBean);
			getproductstatuscountCall.callService();
			int asInt = getproductstatuscountCall.getJsonObject().get("data").getAsJsonObject().get("soldoutNum").getAsInt();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			ModifyAgentProductBean initModifyAgentProductBean = AddAgentProductCallService.initModifyAgentProductBean(productId, addAgentProductBean);
			initModifyAgentProductBean.getCatalogList().forEach(a->{
				a.setStock(0);
				
			});
			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBean);
			Thread.sleep(5000);
			getproductstatuscountBean.setUserId(rootUserId);
			getproductstatuscountCall.setData(getproductstatuscountBean);
			getproductstatuscountCall.callService();
			
			int asInt1 = getproductstatuscountCall.getJsonObject().get("data").getAsJsonObject().get("soldoutNum").getAsInt();
			Logger.verifyEquals(asInt1, asInt+1, "判断售罄商品");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetProductStatusCount_003() {
		Logger.start(true, "减少一个售磬商品，获取商品状态");
		try {
			//设置一个售罄商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(5000);
			ModifyAgentProductBean initModifyAgentProductBean = AddAgentProductCallService.initModifyAgentProductBean(productId, addAgentProductBean);
			initModifyAgentProductBean.getCatalogList().forEach(a->{
				a.setStock(0);
			});
			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBean);
			Thread.sleep(5000);
			getproductstatuscountBean.setUserId(rootUserId);
			getproductstatuscountCall.setData(getproductstatuscountBean);
			getproductstatuscountCall.callService();
			int asInt = getproductstatuscountCall.getJsonObject().get("data").getAsJsonObject().get("soldoutNum").getAsInt();
			//售罄商品设置库存
			initModifyAgentProductBean.getCatalogList().forEach(a->{
				a.setStock(2000);
				
			});
			AddAgentProductCallService.modifyAgentProduct(initModifyAgentProductBean);
			
			Thread.sleep(5000);
			getproductstatuscountBean.setUserId(rootUserId);
			getproductstatuscountCall.setData(getproductstatuscountBean);
			getproductstatuscountCall.callService();
			
			
			int asInt1 = getproductstatuscountCall.getJsonObject().get("data").getAsJsonObject().get("soldoutNum").getAsInt();
//			Logger.verifyEquals(asInt1, asInt+1, "判断售罄商品");
			System.out.println(asInt +"   " +asInt1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}